<template>
  <span 
    :class="[
      'bx-badge',
      {
        'bx-badge--dot': dot,
        'bx-badge--hidden': !showBadge,
        [`bx-badge--${type}`]: type
      }
    ]"
  >
    <slot></slot>
    <sup 
      v-if="showBadge"
      :class="[
        'bx-badge__content',
        'bx-glass',
        {
          'bx-badge__content--dot': dot,
          'bx-badge__content--fixed': $slots.default
        }
      ]"
      :style="badgeStyle"
    >
      <slot name="content">
        {{ displayContent }}
      </slot>
    </sup>
  </span>
</template>

<script>
import { computed } from 'vue'

export default {
  name: 'BxBadge',
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    max: {
      type: Number,
      default: 99
    },
    dot: {
      type: Boolean,
      default: false
    },
    hidden: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'danger',
      validator: (value) => ['primary', 'success', 'warning', 'danger', 'info'].includes(value)
    },
    showZero: {
      type: Boolean,
      default: false
    },
    offset: {
      type: Array,
      default: () => [0, 0]
    }
  },
  setup(props) {
    const showBadge = computed(() => {
      if (props.hidden) return false
      if (props.dot) return true
      if (props.value === 0 || props.value === '0') {
        return props.showZero
      }
      return props.value !== '' && props.value !== null && props.value !== undefined
    })

    const displayContent = computed(() => {
      if (props.dot) return ''
      
      const value = Number(props.value)
      if (!isNaN(value) && value > props.max) {
        return `${props.max}+`
      }
      
      return props.value
    })

    const badgeStyle = computed(() => {
      const [x, y] = props.offset
      const style = {}
      
      if (x !== 0) {
        style.transform = `translateX(${x}px)`
      }
      if (y !== 0) {
        style.transform = style.transform 
          ? `${style.transform} translateY(${y}px)`
          : `translateY(${y}px)`
      }

      return style
    })

    return {
      showBadge,
      displayContent,
      badgeStyle
    }
  }
}
</script>

<style scoped>
.bx-badge {
  position: relative;
  display: inline-block;
  line-height: 1;
}

.bx-badge__content {
  position: absolute;
  top: 0;
  right: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 6px;
  font-size: 12px;
  font-weight: 600;
  line-height: 1;
  color: var(--bx-text-primary);
  background: var(--bx-danger);
  border: 1px solid rgba(255, 59, 48, 0.4);
  border-radius: 9px;
  backdrop-filter: var(--bx-glass-blur);
  box-shadow: 0 2px 8px rgba(255, 59, 48, 0.3);
  transform: translate(50%, -50%);
  transform-origin: center;
  transition: all var(--bx-transition-medium);
  font-family: var(--bx-font-family);
  white-space: nowrap;
  z-index: 10;
}

.bx-badge__content--fixed {
  position: absolute;
}

.bx-badge__content--dot {
  min-width: 8px;
  width: 8px;
  height: 8px;
  padding: 0;
  border-radius: 50%;
}

/* 类型样式 */
.bx-badge--primary .bx-badge__content {
  background: var(--bx-primary);
  border-color: rgba(0, 122, 255, 0.4);
  box-shadow: 0 2px 8px rgba(0, 122, 255, 0.3);
}

.bx-badge--success .bx-badge__content {
  background: var(--bx-success);
  border-color: rgba(52, 199, 89, 0.4);
  box-shadow: 0 2px 8px rgba(52, 199, 89, 0.3);
}

.bx-badge--warning .bx-badge__content {
  background: var(--bx-warning);
  border-color: rgba(255, 149, 0, 0.4);
  box-shadow: 0 2px 8px rgba(255, 149, 0, 0.3);
}

.bx-badge--danger .bx-badge__content {
  background: var(--bx-danger);
  border-color: rgba(255, 59, 48, 0.4);
  box-shadow: 0 2px 8px rgba(255, 59, 48, 0.3);
}

.bx-badge--info .bx-badge__content {
  background: rgba(142, 142, 147, 0.25);
  border-color: rgba(142, 142, 147, 0.4);
  box-shadow: 0 2px 8px rgba(142, 142, 147, 0.3);
}

/* 隐藏状态 */
.bx-badge--hidden .bx-badge__content {
  opacity: 0;
  transform: translate(50%, -50%) scale(0);
}

/* 动画效果 */
.bx-badge__content {
  animation: bx-badge-zoom-in 0.3s ease-out;
}

@keyframes bx-badge-zoom-in {
  0% {
    opacity: 0;
    transform: translate(50%, -50%) scale(0);
  }
  100% {
    opacity: 1;
    transform: translate(50%, -50%) scale(1);
  }
}

/* 脉冲效果 */
.bx-badge__content::after {
  content: '';
  position: absolute;
  top: -1px;
  left: -1px;
  right: -1px;
  bottom: -1px;
  border-radius: inherit;
  background: inherit;
  opacity: 0.3;
  animation: bx-badge-pulse 2s ease-in-out infinite;
}

@keyframes bx-badge-pulse {
  0%, 100% {
    transform: scale(1);
    opacity: 0.3;
  }
  50% {
    transform: scale(1.2);
    opacity: 0;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-badge__content {
    min-width: 16px;
    height: 16px;
    font-size: 11px;
    border-radius: 8px;
  }
  
  .bx-badge__content--dot {
    min-width: 6px;
    width: 6px;
    height: 6px;
  }
}
</style>

